package org.swimv2.servlet;

import java.io.IOException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swimv2.controller.GestioneAmicizia;
import org.swimv2.controller.GestioneAmiciziaRemote;
import org.swimv2.model.Persona;

/**
 * Servlet implementation class RifiutaAmiciziaServlet
 */
public class RifiutaAmiciziaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RifiutaAmiciziaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Persona u = (Persona) request.getSession().getAttribute("utente");
        Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
		props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		Context ctx;
		
		try{
			ctx = new InitialContext(props);
			Object obj = ctx.lookup(GestioneAmicizia.JNDI_NAME);
			GestioneAmiciziaRemote manager = (GestioneAmiciziaRemote)PortableRemoteObject.narrow(obj,GestioneAmiciziaRemote.class);
			String idRichiedente = request.getParameter("username");
			manager.rifiutaAmicizia(idRichiedente, u.getUserID());
			request.getRequestDispatcher("/Notifiche").
			forward(request, response);			

		}
		catch(NamingException e){
			e.printStackTrace();
			
		}
	}

}
